import * as PIXI from 'pixi.js';
import { defaultConfig } from './config';
import { Game } from './game';

// 初始化游戏
window.addEventListener('load', () => {
    // 创建PIXI应用
    const app = new PIXI.Application({
        width: defaultConfig.screenWidth,
        height: defaultConfig.screenHeight,
        backgroundColor: 0x1099bb,
        resolution: window.devicePixelRatio || 1,
        autoDensity: true,
    });

    // 将canvas添加到页面
    const gameCanvas = document.getElementById('gameCanvas');
    if (gameCanvas) {
        gameCanvas.appendChild(app.view as HTMLCanvasElement);
    }

    // 自适应屏幕大小
    const resize = () => {
        const screenWidth = window.innerWidth;
        const screenHeight = window.innerHeight;
        
        // 计算缩放比例
        const scale = Math.min(
            screenWidth / defaultConfig.screenWidth,
            screenHeight / defaultConfig.screenHeight
        );
        
        // 更新canvas大小
        app.renderer.resize(
            defaultConfig.screenWidth * scale,
            defaultConfig.screenHeight * scale
        );
        
        // 更新游戏容器缩放
        app.stage.scale.set(scale);
    };

    // 监听窗口大小变化
    window.addEventListener('resize', resize);
    resize();

    // 创建并启动游戏
    const game = new Game(app);
    game.startGame();
});